﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;

namespace WeChartWorkRobot.Service.Jobs
{
    /// <summary>
    /// 定时任务管理对象
    /// </summary>
    public static class JobManager
    {
        private static ISchedulerFactory s_schedulerFactory;
        private static IScheduler s_scheduler;


        public static void Initialize()
        {
            s_schedulerFactory = new StdSchedulerFactory();
            s_scheduler = s_schedulerFactory.GetScheduler();

            s_scheduler.Start();
        }
        public static void Initialize(int? threadCount)
        {
            var props = new System.Collections.Specialized.NameValueCollection() { };
            if (threadCount.HasValue && threadCount.Value > 0)
            {
                props.Add("quartz.threadPool.ThreadCount", $"{threadCount}");   //设置线程池个数
            }
            s_schedulerFactory = new StdSchedulerFactory(props);
            s_scheduler = s_schedulerFactory.GetScheduler();

            s_scheduler.Start();
        }

        /// <summary>
        /// 添加一个定时执行的任务
        /// 返回任务唯一标识
        /// </summary>
        /// <param name="job"></param>
        /// <param name="schedule"></param>
        public static Guid AddJob<T>(Guid jobid, TriggerBase schedule, IDictionary<string, object> dataDic = null) where T : IJob, new()
        {
            Console.WriteLine("创建作业：" + jobid);
            LogHelper.Debug("创建作业：" + jobid);

            var jobBuilder = JobBuilder.Create<QuaterzJob<T>>()
                .WithIdentity(jobid.ToString());

            IDictionary<string, object> data = dataDic;
            if (data == null)
            {
                data = new Dictionary<string, object>();
            }

            data["__schedule"] = schedule;
            jobBuilder = jobBuilder.SetJobData(new JobDataMap(data));

            var jobDetail = jobBuilder.Build();

            var trigger = schedule.GetQuartzTrigger();

            s_scheduler.ScheduleJob(jobDetail, trigger);

            return jobid;
        }

        /// <summary>
        /// 添加一个定时执行的任务
        /// 返回任务唯一标识
        /// </summary>
        /// <param name="job"></param>
        /// <param name="schedule"></param>
        public static Guid AddJob<T>(TriggerBase schedule, IDictionary<string, object> data = null) where T : IJob, new()
        {
            return AddJob<T>(Guid.NewGuid(), schedule, data);
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="jobId"></param>
        public static void RemoteJob(Guid jobId)
        {
            Console.WriteLine("移除作业：" + jobId);
            s_scheduler.DeleteJob(new JobKey(jobId.ToString()));
        }

        /// <summary>
        /// 关闭全部的任务，用于服务停止前
        /// </summary>
        public static void Shutdown()
        {
            s_scheduler.Shutdown();
        }
    }
}
